using System;
using System.Web;
using System.Web.Caching;
using System.Collections;

namespace CS2K2CatalogSystem
{
	/// <summary>
	/// CacheManager to manage caching of Catalog data.
	/// </summary>
	public class CacheManager
	{
		public CacheManager()
		{

		}

		/// <summary>
		/// Add item to cache
		/// </summary>
		/// <param name="key"></param>
		/// <param name="keyData"></param>
		public void Add(string key, object keyData)
		{
			HttpContext.Current.Cache.Insert(key, keyData, null, DateTime.Now.AddMinutes(5), TimeSpan.Zero);
		}

		/// <summary>
		/// Clear the cache
		/// </summary>
		public void Flush()
		{
			IDictionaryEnumerator enumarator = HttpContext.Current.Cache.GetEnumerator();
			
			while(enumarator.MoveNext())
			{
				HttpContext.Current.Cache.Remove(enumarator.Key.ToString());
			}
		}

		/// <summary>
		/// Get item from cache
		/// </summary>
		/// <param name="key"></param>
		/// <returns></returns>
		public object GetData(string key)
		{
			return HttpContext.Current.Cache.Get(key);
		}

		/// <summary>
		/// Remove item form cache
		/// </summary>
		/// <param name="key"></param>
		public void Remove(string key)
		{
			HttpContext.Current.Cache.Remove(key);
		}
	}
}
